from django.contrib.auth.models import User
from cis.wsga import cliente

class SgaAuthBackend:

    def authenticate(self, username=None, password=None):
        try:
            """
            autenticacion SGA:
            """
            wsdlFile = 'http://miltonlab:1000lab@ws.unl.edu.ec/sgaws/wspersonal/soap/api.wsdl'
            cliente.myHTTPTransport.setAuthentication('miltonlab', '1000lab')
            server = cliente.WSDL.Proxy(wsdlFile, transport=cliente.myHTTPTransport)
            docente = server.sgaws_datos_docente(cedula=username)

            print docente

            # solo probando
            username = 'miltonlab'
            password = 'miltonlab'
            #
            user = User.objects.get(username=username)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None
        except Exception as ex:
            print 'Error ', ex
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DosNotExist:
            return None
